<?php

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
/**
 * Description of QuestionController
 *
 * @author hoangle
 */
class QuestionController extends Zend_Controller_Action {

    private $sessionLogin;
    
    public function init() {
        /*
        //Check session
        $this->sessionLogin = new Zend_Session_Namespace('User');
        //Check session
        if (empty($this->sessionLogin->accountinfo)) {
            $this->_redirect('/');
            exit;
        }*/        
    }
    
    public function indexAction() {
        $this->view->headTitle('Question | Front End');
        if ($this->_request->isGet()) {
            $arrData = $this->_request->getParams();
        }		
        //$userid = $this->sessionLogin->accountinfo['userid'];
	$cateid = $arrData['cateid'];
        //Get category
        $arrCategory = (array) Service_Model::factory('Category', 'getCategoryById', $cateid, 'frontend');
        //Get hot question
        $arrHotQuestion = (array) Service_Model::factory('Question', 'getHotQuestion', $cateid, 'frontend');
        /*foreach ($arrQuestion as $val) {
            $arrRestQuest[$val['mackid']] = $val['mackname'];
        }*/
        //Get open question 
        $arrOpenQuestion = (array) Service_Model::factory('Question', 'getOpenQuestion', $cateid, 'frontend');

        //Get open question
        $arrEventQuestion = (array) Service_Model::factory('Event', 'getEventQuestion', $cateid, 'frontend');

        $this->view->category = $arrCategory;
        $this->view->host_question = $arrHotQuestion;
        $this->view->open_question = $arrOpenQuestion;
        $this->view->event_question = $arrEventQuestion;
    }

    public function categoryAction() {
        $this->view->headTitle('Question | Front End');
        if ($this->_request->isGet()) {
            $arrData = $this->_request->getParams();
        }
        //$userid = $this->sessionLogin->accountinfo['userid'];
	$cateid = $arrData['cateid'];
        //Get category
        $arrCategory = (array) Service_Model::factory('Category', 'getCategoryById', $cateid, 'frontend');
        //Get hot question
        $arrHotQuestion = (array) Service_Model::factory('Question', 'getHotQuestion', $cateid, 'frontend');
        //Get open question
        $arrOpenQuestion = (array) Service_Model::factory('Question', 'getOpenQuestion', $cateid, 'frontend');

        //Get open question
        $arrEventQuestion = (array) Service_Model::factory('Event', 'getEventQuestion', $cateid, 'frontend');

        $this->view->category = $arrCategory;
        $this->view->host_question = $arrHotQuestion;
        $this->view->open_question = $arrOpenQuestion;
        $this->view->event_question = $arrEventQuestion;
    }

    public function addAction() {
        $this->view->headTitle('Add Question | Front End');
        //$userid = $this->sessionLogin->accountinfo['userid'];

        //Get category
        $arrCategory = (array) Service_Model::factory('Category', 'getCategory', NULL, 'frontend');
        $arrParrentCate = array();
        $arrChildCateOption = array();
        //var_dump($arrCategory);
        foreach ($arrCategory as $cate) {
            if($cate['cate_up_level']==0){
                    $arrParrentCate = $cate;
            }
            else{
                    $arrChildCateOption = $cate;
            }
        }
        //Get question user
        //$arrUserQuestion = (array) Service_Model::factory('Question', 'getUserQuestionByUserId', $userid, 'frontend');        
         $this->view->parrent_category = $arrParrentCate;
    }
    public function saveaddAction() {
        $this->view->headTitle('Add Question | Front End');
        //Check data submit
        if ($this->_request->isPost()) {
            $arrData = $this->_request->getPost();
            //$userid = $this->sessionLogin->accountinfo['userid'];
            $userid = 1;
            $arrData['userid'] = $userid;
            //Validate data input
            $errorBool = FALSE;
            $this->_validateRegister($arrData, $errorBool);
            //Insert data to DB
            if (!$errorBool) {
                
                if (!empty($arrData)) {
                    $arrInput = NULL;
                    $arrInput = array(
                        //'userid' => $userid,
                        'quest_title' => $arrData['quest_title'],
                        'quest_content' => $arrData['quest_content'],
                        'cate_id' => $arrData['cate_id'],
                        'user_pick' => NULL,
                        'is_open' => 1,
                	'ask_type' => 1,
                        'date_ask' => date('Y-m-d H:i:s'),
                        'prize_give' => $arrData['prize_give']
                    );
                    $result = Service_Model::factory('Question', 'insertQuestion', $arrInput, 'frontend');
                }

                //End for
                if ($result) {
                    $this->_redirect('question/success');
                }
            }
            else{
                    echo $this->view->errorQuest;die;
            }
            //End if
        }
        //End post

    }

    /**
     * @desc insert question of user
     */
    public function setAction() {
        //Check data submit
        if ($this->_request->isPost()) {
            $arrData = $this->_request->getPost();

            $arrData['mack'] = array_unique($arrData['mack']);

            $userid = $this->sessionLogin->accountinfo['userid'];
            $arrData['userid'] = $userid;
            //Validate data input
            $this->_validateRegister($arrData, $errorBool);
            
            //Insert data to DB
            if (!$errorBool) {
                foreach ($arrData['mack'] as $k => $qid) {
                    if (!empty($qid)) {
                        $arrInput = NULL;
                        $arrInput = array(
                            'userid' => $userid,
                            'questionid' => $qid,
                            'amount' => $arrData['amount'][$k],
                            'total' => $arrData['tongck'][$k]
                        );
                        $result = Service_Model::factory('Question', 'insertUserQuestion', $arrInput, 'frontend');
                    }
                }
                //End for
                if ($result) {
                    $this->_redirect('question/success');
                }
            }
            //End if
        }
        //End post

        $arrQuestion = (array) Service_Model::factory('Question', 'getQuestion', NULL, 'frontend');
        $this->view->question = $arrQuestion;
    }

//End set func
    /**
     * @desc Report set question success
     */
    public function successAction() {
        $this->view->flag = 'Bạn đã đặt câu hỏi thành công, vui lòng theo dõi thông tin kết quả tư vấn từ hệ thống.';
    }
    
    public function listAction() {
        $userid = $this->sessionLogin->accountinfo['userid'];

        //Get question
        $arrQuestion = (array) Service_Model::factory('Question', 'getQuestion', NULL, 'frontend');
        foreach ($arrQuestion as $val) {
            $arrRestQuest[$val['mackid']] = $val['mackname'];
        }

        //Get question user
        $arrUserQuestion = (array) Service_Model::factory('Question', 'getUserQuestionByUserId', $userid, 'frontend');

        $this->view->question = $arrRestQuest;
        $this->view->userquestion = $arrUserQuestion;
    }

    /**
     * @desc Refuse
     */
    public function refuseAction() {
        $userid = $this->sessionLogin->accountinfo['userid'];

        $id = $this->_request->getParam('id');

        $arrUserQuestion = (array) Service_Model::factory('UserQuestion', 'getUserQuestionById', $id, 'frontend');

        $createtime = date('Y-m-d', strtotime($arrUserQuestion['createtime']));

        //Get question user
        if (date('Y-m-d') != $createtime) {
            $result = (array) Service_Model::factory('UserQuestion', 'updateUserQuestion',
                            array('userid' => $userid, 'id' => $id), 'frontend');
            if ($result) {
                //Balance
                if ($arrUserQuestion['count_refuse'] <= 1) {
                    $result = Service_Model::factory('Balance', 'creaseCreditByUserId', $userid, 'frontend');
                }
            }
        }

        $this->_redirect('question/list');
    }



    ////////////////////////////////////////////////////////////////////////////
    //                          PRIVATE FUNC                                  //
    ////////////////////////////////////////////////////////////////////////////
    /**
     * @desc Check validate data input
     * @param <type> $arrData
     * @param <type> $errorBool
     */
    private function _validateRegister(& $arrData, & $errorBool) {
        $errorBool = false;
        $arrResult = array();
        $i = 0;
        
        //Check validate tiêu đề
        if (empty($arrData['quest_title'])) {
            $errorBool = true;
            $this->view->errorQuest = 'Hãy nhập Tiêu Đề';
        }
        //End mack

        //Check validate CONTENT
        if (empty($arrData['quest_content'])) {
            $errorBool = true;
            $this->view->errorQuest = 'Hãy nhập Nội Dung';
        }
        //End so luong

        if ($errorBool) {
            $this->view->errorFlag = true;
        }
    }
    function show_similar_quest(){
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
    }
//End func
}

?>
